package com.kgcx.coupon.controller;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.controller.BaseController;
import com.kgcx.common.exception.BusinessException;
import com.kgcx.common.exception.CoreErrors;
import com.kgcx.common.log.Log;
import com.kgcx.coupon.service.AdvertService;
import com.kgcx.coupon.vo.ad.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author panjin.
 * @date 2017/12/15.
 */
@Api(description = "广告接口", tags = "广告接口")
@RestController
@RequestMapping("advert")
@Slf4j
public class AdvertController extends BaseController {

    @Autowired
    public AdvertService advertService;

    /**
     * 获取广告
     * @param place 广告位
     * @return
     */
    @ApiOperation(value = "客户端广告", notes = "客户端广告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "place", value = "广告位", required = true, paramType = "query", dataType = "Byte", dataTypeClass = Byte.class)
    })
    @GetMapping("getAdvert")
    public ReturnData<List<AdImage>> getAdvert(@RequestParam Byte place) {
        if(log.isInfoEnabled()) {
            log.info(Log.op("客户端获取广告").kv("request", place).toString());
        }
        List<AdImage> adImageList = advertService.getAdvert(place);
        if (null == adImageList) {
            return ReturnData.getResult();
        }
        return new ReturnData<>(adImageList);
    }

    /**
     * 分页查询广告信息
     * @param adVertQueryReqVO 广告查询分页请求对象
     * @return
     */
    @ApiOperation(value = "广告分页查询", httpMethod = "POST", notes = "广告分页查询接口")
    @PostMapping(value = "queryListWithPage")
    public ReturnData<List<AdVertQueryRespVO>> queryListWithPage(@RequestBody AdVertQueryReqVO adVertQueryReqVO) {
        if(log.isInfoEnabled()) {
            log.info(Log.op("分页查询广告").kv("request", adVertQueryReqVO).toString());
        }
        PageHelper.startPage(adVertQueryReqVO.getPageNum(),adVertQueryReqVO.getPageSize());
        ReturnData<List<AdVertQueryRespVO>> returnData = ReturnData.getResult();
        returnData.setData(advertService.queryListWithPage(adVertQueryReqVO));
        return returnData;
    }

    /**
     * 添加广告
     * @param advertReqVO 新增广告对象
     * @return
     */
    @ApiOperation(value = "添加广告", notes = "添加单条广告")
    @PostMapping(value = "addAdvert")
    public ReturnData addAdvert(@RequestBody AdvertReqVO advertReqVO) {
        if(log.isInfoEnabled()) {
            log.info(Log.op("添加广告").kv("request", advertReqVO).toString());
        }
        try {
            advertService.add(advertReqVO);
        } catch (Exception e) {
            log.error(Log.op("addAdvert").msg("add advert error").kv("request", advertReqVO).toString(), e);
            if(e instanceof BusinessException) {
                return new ReturnData(((BusinessException) e).getCode(), e.getMessage());
            }
            return new ReturnData(CoreErrors.SYSTEM_ERROR.getCode(), CoreErrors.SYSTEM_ERROR.getMessage());
        }
        return ReturnData.getResult();
    }

    /**
     * 广告详情
     * @return
     */
    @ApiOperation(value = "广告详情", notes = "广告详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "主键id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @GetMapping("queryAdvert")
    public ReturnData queryAdvert(@RequestParam Integer kid) {
        if(log.isInfoEnabled()) {
            log.info(Log.op("添加广告").kv("request", kid).toString());
        }
        try {
            AdvertRespVO advert = advertService.queryAdvert(kid);
            return new ReturnData<>(advert);
        } catch (Exception e) {
            log.error(Log.op("queryAdvert").msg("query advert error").kv("request", kid).toString(), e);
            if(e instanceof BusinessException) {
                return new ReturnData(((BusinessException) e).getCode(), e.getMessage());
            }
            return new ReturnData(CoreErrors.SYSTEM_ERROR.getCode(), CoreErrors.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 更新广告
     * @param advertUpdateReqVO
     * @return
     */
    @ApiOperation(value = "更新广告", notes = "更新单条广告")
    @PostMapping(value = "updateAdvert")
    public ReturnData updateAdvert(@RequestBody AdvertUpdateReqVO advertUpdateReqVO) {
        if(log.isInfoEnabled()) {
            log.info(Log.op("更新广告").kv("request", advertUpdateReqVO).toString());
        }
        try {
            advertService.updateAdvert(advertUpdateReqVO);
        } catch (Exception e) {
            log.error(Log.op("updateAdvert").msg("update advert error").kv("request", advertUpdateReqVO).toString(), e);
            if(e instanceof BusinessException) {
                return new ReturnData(((BusinessException) e).getCode(), e.getMessage());
            }
            return new ReturnData(CoreErrors.SYSTEM_ERROR.getCode(), CoreErrors.SYSTEM_ERROR.getMessage());
        }
        return ReturnData.getResult();
    }

    /**
     * 删除单条广告
     * @param kid 广告ID
     * @return
     */
    @ApiOperation(value = "删除广告", notes = "删除单条广告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kid", value = "主键id", required = true, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class)
    })
    @PostMapping(value = "deleteAdvert")
    public ReturnData deleteAdvert(@RequestParam Integer kid) {
        log.info(Log.op("删除广告").msg("start").kv("request", kid).toString());
        advertService.deleteAdvert(kid);
        log.info(Log.op("删除广告").msg("end").kv("request", kid).toString());
        return ReturnData.getResult();
    }

    /**
     * 批量删除广告
     * @param kids 广告ID列表
     * @return
     */
    @ApiOperation(value = "批量删除广告", notes = "批量删除广告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "kids", value = "主键id列表", required = false, paramType = "query", dataType = "Integer", allowMultiple = true)
    })
    @GetMapping(value = "deleteAdvertBatch")
    public ReturnData deleteAdvertBatch(@RequestParam List<Integer> kids) {
        log.info(Log.op("批量删除广告").msg("start").kv("request", kids).toString());
        advertService.deleteAdvertBatch(kids);
        log.info(Log.op("批量删除广告").msg("end").kv("request", kids).toString());
        return ReturnData.getResult();
    }
}
